from django.template import RequestContext
from django.shortcuts import render_to_response
# Avoid shadowing the login() and logout() views below.
from django.contrib.auth import REDIRECT_FIELD_NAME, login as auth_login, logout as auth_logout
from django.contrib.auth import authenticate

def login(request):
    extra_context = None
    current_app = None
    state = "Please login ..."
    username = password = ''
    if request.POST:
        username = request.POST.get('username')
        password = request.POST.get('password')
        db = request.POST.get('db')

        user = authenticate(username=username, password=password, db=db)
        if user is not None:
            if user.is_active:
                auth_login(request, user)
                state = "You're successfully logged in"
            else:
                state = "Your account is not active"
        else:
            state = "Your username and/or password were incorrect"

    context = {
            'state': state,
            'username': username, 
    }
    context.update(extra_context or {})
    return render_to_response("base.html", context,
                              context_instance=RequestContext(request, current_app=current_app))
